package com.randi.file.controller;

import com.randi.common.annotation.LogAnnotation;
import com.randi.common.core.domain.Result;
import com.randi.common.enums.BusinessType;
import com.randi.common.enums.ResultCode;
import com.randi.file.entity.vo.FileSliceMergeVo;
import com.randi.file.entity.vo.FileSliceUploadCheckVo;
import com.randi.file.entity.vo.FileSliceUploadVo;
import com.randi.file.entity.vo.FileUploadVo;
import com.randi.file.enums.FileSliceMergeStatusEnum;
import com.randi.file.service.ISysFileUploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @author ziye
 */
@RestController
@RequestMapping("/file/upload")
@Api(value = "file", tags = "文件上传")
public class SysFileUploadController {

    @Resource
    private ISysFileUploadService iSysFileUploadService;

    /**
     * 文件上传测速
     *
     * @return Result
     */
    @PostMapping("/speed")
    @ApiOperation(value = "文件上传测速")
    @LogAnnotation(module = "文件上传管理", value = "分片上传校验", businessType = BusinessType.TEST)
    public Result uploadSpeed(@RequestParam("file") MultipartFile file) {
        return Result.success();
    }

    @PostMapping("/simple")
    @ApiOperation(value = "文件上传")
    @LogAnnotation(module = "文件上传管理", value = "文件上传", businessType = BusinessType.UPLOAD)
    public Result fileUpload(@ModelAttribute @Validated FileUploadVo fileUpload) throws IOException {
        String savePath = iSysFileUploadService.simpleUpload(fileUpload);
        if (savePath == null) {
            return Result.error(ResultCode.FILE_UPLOAD_FAIL);
        }
        return Result.success().put(savePath);
    }

    /**
     * 分片上传校验
     *
     * @param fileHash 文件哈希
     * @return Result
     */
    @GetMapping("/slice/check")
    @ApiOperation(value = "分片上传校验")
    @LogAnnotation(module = "文件上传管理", value = "分片上传校验", businessType = BusinessType.CHECK)
    public Result fileUploadCheck(@RequestParam("fileHash") String fileHash) {
        FileSliceUploadCheckVo uploadCheck = iSysFileUploadService.sliceUploadCheck(fileHash);
        if (uploadCheck == null) {
            uploadCheck = new FileSliceUploadCheckVo();
        }
        return Result.success().put(uploadCheck);
    }

    /**
     * 分片上传
     *
     * @param fileChunkUpload fileChunkUploadVo
     * @return Result
     */
    @PostMapping("/slice")
    @ApiOperation(value = "分片上传")
    public Result sliceUpload(@ModelAttribute @Validated FileSliceUploadVo fileChunkUpload) {
        boolean status = iSysFileUploadService.sliceUpload(fileChunkUpload);
        if (!status) {
            return Result.error(ResultCode.FILE_UPLOAD_SLICE_FAIL);
        }
        return Result.success();
    }

    /**
     * 分片合并
     *
     * @param sliceMerge FileSliceMergeVo
     * @return Result
     */
    @PostMapping("/slice/merge")
    @ApiOperation(value = "分片合并")
    @LogAnnotation(module = "文件上传管理", value = "分片合并", businessType = BusinessType.MERGE)
    public Result sliceUpload(@Validated @RequestBody FileSliceMergeVo sliceMerge) {
        String tackId = iSysFileUploadService.sliceMerge(sliceMerge);
        return Result.success().put(tackId);
    }

    /**
     * 分片合并校验
     *
     * @param tackId 任务ID
     * @return Result
     */
    @GetMapping("/slice/verify/{tackId}")
    @ApiOperation(value = "分片合并校验")
    public Result sliceUploadMergeVerify(@PathVariable String tackId) {
        FileSliceMergeStatusEnum status = iSysFileUploadService.sliceMergeVerify(tackId);
        return Result.success().put(status);
    }

}
